package com.itheima.mm.controller;

import com.itheima.mm.constants.Constants;
import com.itheima.mm.entity.Result;
import com.itheima.mm.pojo.ReviewLog;
import com.itheima.mm.pojo.User;
import com.itheima.mm.service.IReviewService;
import com.itheima.mm.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

/**
 * 包名:com.itheima.mm.controller
 * 作者:Leevi
 * 日期2020-02-07  09:35
 */
@RestController
@RequestMapping("review")
public class ReviewController {
    @Autowired
    private IReviewService reviewService;
    @RequestMapping("add")
    public Result add(@RequestBody ReviewLog reviewLog, HttpSession session) throws IOException {
        boolean flag = false;
        try {
            //2.手动设置用户id和审核时间
            //2.1 设置审核时间为当前时间
            reviewLog.setCreateDate(DateUtils.parseDate2String(new Date()));
            //2.2 设置审核的用户id为当前用户的id
            User user = (User) session.getAttribute(Constants.MM_USER_USERNAME);
            reviewLog.setUserId(user.getId());

            //3. 调用业务层的方法添加试题审核
            flag = reviewService.add(reviewLog);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //判断flag的值，如果true，表示审核成功
        if (flag) {
            //审核成功
            return new Result(true,"审核成功");
        }else {
            //审核失败
            return new Result(false,"审核失败");
        }
    }
}
